package com.sinosoft.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.sinosoft.base.BaseFormController;
import com.sinosoft.report.ReportManager;
import com.sinosoft.report.ReportView;
import com.sinosoft.util.CheckEmpty;
import com.sinosoft.util.PropertiesUtil;

@SuppressWarnings("unchecked")
@Controller
public class VisitReportController extends BaseFormController {

	private ReportManager reportMan;

	@Autowired
	public void setReportManager(ReportManager reportMan) {
		this.reportMan = reportMan;
	}

	@RequestMapping("report/visitreport.html")
	protected String setcounturl(HttpServletRequest request,
			HttpServletResponse response) {
		return "report/visitreport";
	}

	/**
	 * 访问量图形统计
	 * 
	 * @param starttime
	 * @param endtime
	 * @param stry
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("report/visitpicreport.html")
	protected ModelAndView countVisitLists(String starttime, String endtime,
			String stry, HttpServletRequest request,
			HttpServletResponse response) {
		String group = stry;
		if (CheckEmpty.isEmpty(stry) || stry.equals("null") || stry.equals("")) {
			group = "yyyy-MM-dd";
		}

		String year = PropertiesUtil.getPropertiesValue("report.visit.year",
				request)[0];
		String month = PropertiesUtil.getPropertiesValue("report.visit.month",
				request)[0];
		String day = PropertiesUtil.getPropertiesValue("report.visit.day",
				request)[0];
		List<ReportView> list = reportMan.searchVisitNum(group, starttime,
				endtime);
		
		for (int i = 0; i < list.size(); i++) {
			String time = list.get(i).getValue();
			if (group.equals("yyyy")) {
				time += year;
				list.get(i).setValue(time);
			} else if (group.equals("yyyy-MM")) {
				time += month;
				list.get(i).setValue(time);
			} else if (group.equals("yyyy-MM-dd")) {
				time += day;
				list.get(i).setValue(time);
			}
		}
		StringBuffer sbvalue = new StringBuffer();
		StringBuffer sbnum = new StringBuffer();
		int count=0;
		for (int i = 0; i < list.size(); i++) {
			ReportView vo = list.get(i);
			count+=vo.getNum();
			if (i == list.size() - 1) {
				sbvalue.append(vo.getValue());
				sbnum.append(vo.getNum());
			} else {
				sbvalue.append(vo.getValue() + "_");
				sbnum.append(vo.getNum() + "_");
			}
		}
		String[] a = new String[] {};
		sbvalue.append(",").append(sbnum);
		JSONObject jsonobj = new JSONObject();
		jsonobj.put("ingredients", sbvalue.toString());
		jsonobj.put("count", count);

		try {
			response.getWriter().write(jsonobj.toString());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
}
